package cn.micai.base.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/20 15:30
 */
public class TestCheckSum {

    public static void main(String [] args) {

        String dataFile = "D://newFile.txt";

        try {
            MessageDigest sha1 = MessageDigest.getInstance("SHA1");
            FileInputStream fileInputStream = new FileInputStream(dataFile);
            byte[] dataBytes = new byte[1024];
            int nread = 0;
            while ((nread = fileInputStream.read(dataBytes)) != -1) {
                sha1.update(dataBytes, 0, nread);
            }

            byte[] digest = sha1.digest();
            // convert the byte to hex format 将字节转换为hex格式
            StringBuffer sb = new StringBuffer("");
            for (int i=0; i<digest.length; i++) {
                sb.append(Integer.toString((digest[i]&0xff) + 0x100, 16).substring(1));
            }
            System.out.println("Digest(in hex format):: " + sb.toString());


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
